package com.well.rpc.event;

import com.alibaba.fastjson.JSONArray;
import com.well.rpc.job.JobHandler;
import com.well.rpc.job.WellJobContext;
import com.well.rpc.model.DoJobHandlerModel;
import com.well.rpc.model.EventType;
import com.well.rpc.model.TransData;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.PostConstruct;

@Slf4j
public class DoWellJobEvent implements AppEvent {

    @Override
    @PostConstruct
    public void addEvent() {
        EventContext.setEvent(EventType.DO_JOB, this);
    }

    @Override
    public void doEvent(ChannelHandlerContext channelHandlerContext, TransData transData) {
        String jsonData = transData.getJsonData();
        DoJobHandlerModel model = JSONArray.parseObject(jsonData, DoJobHandlerModel.class);
        WellJobContext context = new WellJobContext();
        context.setParam(model.getParam());
        log.info("do job: " + model.getJobName());
        JobHandler.doJob(model, context);
    }

    @Override
    public EventType eventType() {
        return EventType.DO_JOB;
    }

    @Override
    public void errorEvent(ChannelHandlerContext channelHandlerContext, Throwable cause) {

    }
}
